package com.jiesen.base.modules.login.branch.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.baseDao.RedisServiceImpl;
import com.jiesen.base.modules.login.branch.model.Branch;
import com.jiesen.base.modules.login.branch.service.BranchService;
import com.jiesen.base.modules.login.jsLoginUser.model.JsLoginUser;
import com.jiesen.base.modules.login.jsRole.model.JsRole;
import com.jiesen.base.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/branch")
public class BranchController {

    @Autowired
    private BranchService branchService;

    @Autowired
    private RedisServiceImpl redisService;

    //增加一个分校或者大区的信息
    @PostMapping("/create")
    public R create(@RequestBody Branch branch) {
        boolean save = branchService.save(branch);
        if (!save) {
            return R.error();
        }
        return R.ok();
    }

    //删除
    @GetMapping("/delete")
    public R delete(@RequestParam("id") Integer id) {
        boolean b = branchService.removeById(id);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    //修改
    @PostMapping("/edit")
    public R edit(@RequestBody Branch branch) {
        boolean b = branchService.updateById(branch);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    //查询单个
    @GetMapping("/view")
    public R view(@RequestParam("id") Integer id) {
        Branch branch = branchService.getById(id);
        return R.ok().put("data", branch);
    }

    //条件查询
    @PostMapping("/view")
    public R view(@RequestBody Branch branch) {
        return branchService.page(branch);
    }

    //根据大区id查询分校信息
    @GetMapping("/viewBranch")
    public R viewBranch(@RequestParam("parentId") Integer id) {
        QueryWrapper<Branch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        List<Branch> list = branchService.list(queryWrapper);
        return R.ok().put("data", list);
    }

    //查询所有的大区信息
    @GetMapping("/viewArea")
    public R viewArea(HttpServletRequest request) {
        //根据token查询所属分校
        String token = request.getHeader("token");
        JsLoginUser jsLoginUser = (JsLoginUser) redisService.get(token);
        List<JsRole> jsRoles = jsLoginUser.getJsRoles();

        ArrayList<Branch> branches = new ArrayList<>();


            Integer branchId = jsLoginUser.getBranchId();
            if (branchId == null) {
                return R.error("没有所属校区");
            }
            if (branchId == 0) {
                QueryWrapper<Branch> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("parent_id", 0);
                List<Branch> list = branchService.list(queryWrapper);
                for (Branch branch : list) {
                    QueryWrapper<Branch> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("parent_id", branch.getId());
                    List<Branch> list1 = branchService.list(queryWrapper1);
                    branch.setBranchs(list1);
                }
                return R.ok().put("data", list);
            }
            Branch branch = branchService.getById(branchId);

            if (branch.getParentId() == 0) {
                //如果是大区信息，则添加所有的分校信息
                QueryWrapper<Branch> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("parent_id", branch.getId());
                List<Branch> list = branchService.list(queryWrapper);
                branch.setBranchs(list);
                branches.add(branch);
            } else {
                //如果是分校，则返回大区加分校信息
                boolean b = true;
                for (Branch branch1 : branches) {
                    if (branch1.getId().equals(branch.getParentId())) {
                        List<Branch> branchs = branch1.getBranchs();
                        for (Branch branch2 : branchs) {
                            if (branch2.getId().equals(branch.getId())) {
                                b = false;
                            }
                        }
                        if (b) {
                            branchs.add(branch);
                        }
                    }

                }
                if (b) {
                    //没有找到，返回大区加分校信息
                    Branch branch1 = branchService.getById(branch.getParentId());
                    ArrayList<Branch> branche1 = new ArrayList<>();
                    branche1.add(branch);
                    branch1.setBranchs(branche1);
                    branches.add(branch1);
                }
            }
        return R.ok().put("data", branches);
    }

}
